package org.meb.lotrlcg.web;

import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Singleton;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

@Singleton
public class PersistenceProducer {

	private EntityManagerFactory emf;

	@Produces
	@RequestScoped
	public EntityManager createEntityManager() {
		return createEntityManagerFactory().createEntityManager();
	}

	private EntityManagerFactory createEntityManagerFactory() {
		if (emf == null) {
			emf = Persistence.createEntityManagerFactory("lotrlcgUnit");
		}
		return emf;
	}
}
